package bb.lanxing.lib.devices.bryton.ncs;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExtensionManager {
    private static ExtensionManager sInstance;
    private final Context mApplicationContext;
    private final List<ExtensionWithData> mActiveExtensions = new ArrayList();
    private Map<ComponentName, ExtensionWithData> mExtensionInfoMap = new HashMap();
    private List<OnChangeListener> mOnChangeListeners = new ArrayList();
    private Handler mMainThreadHandler = new Handler(Looper.getMainLooper());

    public static class ExtensionListing {
        public ComponentName componentName;
        public String description;
        public Drawable icon;
        public int protocolVersion;
        public ComponentName settingsActivity;
        public String title;
        public boolean worldReadable;
    }

    public static class ExtensionWithData {
        public ExtensionData latestData;
        public ExtensionListing listing;
    }

    public interface OnChangeListener {
        void onExtensionsChanged(ComponentName componentName);
    }

    private void destroyExtensionData(ComponentName componentName) {
    }

    public static ExtensionManager getInstance(Context context) {
        if (sInstance == null && context != null) {
            sInstance = new ExtensionManager(context);
        }
        return sInstance;
    }

    private ExtensionManager(Context context) {
        this.mApplicationContext = context.getApplicationContext();
    }

    public List<ExtensionWithData> getActiveExtensionsWithData() {
        ArrayList arrayList;
        synchronized (this.mActiveExtensions) {
            arrayList = new ArrayList(this.mActiveExtensions);
        }
        return arrayList;
    }

    public List<ComponentName> getActiveExtensionNames() {
        ArrayList arrayList = new ArrayList();
        for (ExtensionWithData extensionWithData : this.mActiveExtensions) {
            arrayList.add(extensionWithData.listing.componentName);
        }
        return arrayList;
    }

    public List<ExtensionListing> getAvailableExtensions() {
        ArrayList arrayList = new ArrayList();
        PackageManager packageManager = this.mApplicationContext.getPackageManager();
        for (ResolveInfo resolveInfo : packageManager.queryIntentServices(new Intent(NotificationExtension.ACTION_EXTENSION), 128)) {
            ExtensionListing extensionListing = new ExtensionListing();
            if (resolveInfo.serviceInfo.packageName.equals(this.mApplicationContext.getPackageName())) {
                extensionListing.componentName = new ComponentName(resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name);
                extensionListing.title = resolveInfo.loadLabel(packageManager).toString();
                Bundle bundle = resolveInfo.serviceInfo.metaData;
                if (bundle != null) {
                    extensionListing.protocolVersion = bundle.getInt("protocolVersion");
                    extensionListing.worldReadable = bundle.getBoolean("worldReadable", false);
                    extensionListing.description = bundle.getString("description");
                    String string = bundle.getString("settingsActivity");
                    if (!TextUtils.isEmpty(string)) {
                        extensionListing.settingsActivity = ComponentName.unflattenFromString(resolveInfo.serviceInfo.packageName + "/" + string);
                    }
                }
                extensionListing.icon = resolveInfo.loadIcon(packageManager);
                arrayList.add(extensionListing);
            }
        }
        return arrayList;
    }

    public boolean cleanupExtensions() {
        ArrayList arrayList = new ArrayList();
        for (ExtensionListing extensionListing : getAvailableExtensions()) {
            arrayList.add(extensionListing.componentName);
        }
        setActiveExtensions(arrayList);
        return true;
    }

    public void setActiveExtensions(List<ComponentName> list) {
        setActiveExtensions(list, true);
    }

    private void setActiveExtensions(List<ComponentName> list, boolean z) {
        HashMap hashMap = new HashMap();
        for (ExtensionListing extensionListing : getAvailableExtensions()) {
            hashMap.put(extensionListing.componentName, extensionListing);
        }
        List<ComponentName> activeExtensionNames = getActiveExtensionNames();
        if (activeExtensionNames.equals(list)) {
            return;
        }
        for (ComponentName componentName : activeExtensionNames) {
            if (!list.contains(componentName)) {
                destroyExtensionData(componentName);
            }
        }
        ArrayList<ExtensionWithData> arrayList = new ArrayList();
        for (ComponentName componentName2 : list) {
            if (this.mExtensionInfoMap.containsKey(componentName2)) {
                arrayList.add(this.mExtensionInfoMap.get(componentName2));
            } else {
                ExtensionWithData extensionWithData = new ExtensionWithData();
                extensionWithData.listing = (ExtensionListing) hashMap.get(componentName2);
                if (extensionWithData.listing == null) {
                    extensionWithData.listing = new ExtensionListing();
                    extensionWithData.listing.componentName = componentName2;
                }
                extensionWithData.latestData = deserializeExtensionData(extensionWithData.listing.componentName);
                arrayList.add(extensionWithData);
            }
        }
        this.mExtensionInfoMap.clear();
        for (ExtensionWithData extensionWithData2 : arrayList) {
            this.mExtensionInfoMap.put(extensionWithData2.listing.componentName, extensionWithData2);
        }
        synchronized (this.mActiveExtensions) {
            this.mActiveExtensions.clear();
            this.mActiveExtensions.addAll(arrayList);
        }
    }

    private ExtensionData deserializeExtensionData(ComponentName componentName) {
        return new ExtensionData();
    }

    public boolean updateExtensionData(ComponentName componentName, ExtensionData extensionData) {
        extensionData.truncate();
        ExtensionWithData extensionWithData = this.mExtensionInfoMap.get(componentName);
        if (extensionWithData == null || ExtensionData.equals(extensionWithData.latestData, extensionData)) {
            return false;
        }
        extensionWithData.latestData = extensionData;
        notifyOnChangeListeners(extensionWithData.listing.componentName);
        return true;
    }

    public void addOnChangeListener(OnChangeListener onChangeListener) {
        this.mOnChangeListeners.add(onChangeListener);
    }

    public void removeOnChangeListener(OnChangeListener onChangeListener) {
        this.mOnChangeListeners.remove(onChangeListener);
    }

    private void notifyOnChangeListeners(final ComponentName componentName) {
        this.mMainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                for (OnChangeListener onChangeListener : ExtensionManager.this.mOnChangeListeners) {
                    onChangeListener.onExtensionsChanged(componentName);
                }
            }
        });
    }
}
